package com.gensdai.leliang.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gensdai.leliang.R;
import com.gensdai.leliang.activity.LogisticsInfoActivity;
import com.gensdai.leliang.activity.OrderInfoActivity;
import com.gensdai.leliang.entity.ShopBean;

import java.text.DecimalFormat;
import java.util.List;


/**
 * Created by Administrator on 2017/6/5.
 */

public class Indent_List_Adapter extends BaseAdapter implements View.OnClickListener {

    Context mContext;
    List<ShopBean> mlist;
    Honder honder;
    String indent_state;
    indent_in_list_adapter adapter;


    onItemMenuClick click;

    public void setOnItemMenuClick(onItemMenuClick click) {
        this.click = click;
    }

    public Indent_List_Adapter(Context context, List<ShopBean> list_data, String indent_state) {
        mlist = list_data;
        mContext = context;
        this.indent_state = indent_state;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int positions, View convertView, ViewGroup parent) {
        if (convertView == null) {
            honder = new Honder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.all_indent_list, null);
            honder.cancelIndent = (TextView) convertView.findViewById(R.id.cancel_indent);
            honder.goPayment = (TextView) convertView.findViewById(R.id.go_payment);
            honder.indentNumber = (TextView) convertView.findViewById(R.id.indentNumber);
            honder.all_money = (TextView) convertView.findViewById(R.id.all_money);
            honder.indent_state = (TextView) convertView.findViewById(R.id.indent_state);
            honder.contactSeller = (TextView) convertView.findViewById(R.id.contact_seller);
            honder.tuikuan = (TextView) convertView.findViewById(R.id.tuikuan);
            honder.applybtn = (TextView) convertView.findViewById(R.id.applybtn);
            honder.lookLogistics = (TextView) convertView.findViewById(R.id.look_logistics);
            honder.affirmDelivery = (TextView) convertView.findViewById(R.id.affirm_delivery);
            honder.affirm_evaluate = (TextView) convertView.findViewById(R.id.affirm_evaluate);
            honder.select = (LinearLayout) convertView.findViewById(R.id.select);
            honder.all = (com.gensdai.leliang.view.mylistview) convertView.findViewById(R.id.all);
//            honder.all = (LinearLayout) convertView.findViewById(R.id.all);
            honder.indent_list = (ListView) convertView.findViewById(R.id.indent_list);


//            honder.indent_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                    Intent go_OrderInfo = new Intent(mContext, OrderInfoActivity.class);
//                    go_OrderInfo.putExtra(OrderInfoActivity.EXTRA_ORDER_ID, mlist.get(positions).getIndentId());
//                    Log.e("Indent", "Indent:点击itme:" + positions);
//                    mContext.startActivity(go_OrderInfo);
//                }
//            });

            honder.cancelIndent.setOnClickListener(this);
            honder.goPayment.setOnClickListener(this);
            honder.all.setOnClickListener(this);
            convertView.setTag(honder);
        } else {
            honder = (Honder) convertView.getTag();
        }

        honder.select.setVisibility(View.GONE);
        honder.goPayment.setVisibility(View.GONE);
        honder.affirmDelivery.setVisibility(View.GONE);
        honder.affirm_evaluate.setVisibility(View.GONE);
        honder.lookLogistics.setVisibility(View.GONE);
        honder.cancelIndent.setVisibility(View.GONE);
        honder.tuikuan.setVisibility(View.GONE);
        honder.applybtn.setVisibility(View.GONE);

        if (mlist.get(positions).getIndent_state().equals("1") && mlist.get(positions).getIfFailure().equals("1")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.cancelIndent.setVisibility(View.VISIBLE);
            honder.goPayment.setVisibility(View.VISIBLE);
        } else if (mlist.get(positions).getIndent_state().equals("1") && mlist.get(positions).getIfFailure().equals("0")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.cancelIndent.setVisibility(View.VISIBLE);
        } else if (mlist.get(positions).getIndent_state().equals("3")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.lookLogistics.setVisibility(View.VISIBLE);
            honder.affirmDelivery.setVisibility(View.VISIBLE);
        } else if (mlist.get(positions).getIndent_state().equals("4") && mlist.get(positions).getHasEvaluate().equals("false")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.cancelIndent.setVisibility(View.VISIBLE);
            honder.cancelIndent.setText("删除订单");
            honder.lookLogistics.setVisibility(View.VISIBLE);
            honder.affirm_evaluate.setVisibility(View.VISIBLE);
        } else if (mlist.get(positions).getIndent_state().equals("4") && mlist.get(positions).getHasEvaluate().equals("true")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.cancelIndent.setVisibility(View.VISIBLE);
            honder.cancelIndent.setText("删除订单");
            honder.lookLogistics.setVisibility(View.VISIBLE);
        } else if (mlist.get(positions).getIndent_state().equals("5")) {

        } else if (mlist.get(positions).getIndent_state().equals("2") && mlist.get(positions).getType().equals("0")) {

        }else if (mlist.get(positions).getIndent_state().equals("2") && mlist.get(positions).getType().equals("1")) {
            honder.select.setVisibility(View.VISIBLE);
            honder.tuikuan.setVisibility(View.VISIBLE);
        }else if (mlist.get(positions).getIndent_state().equals("7")) {
            //退款完成
            honder.select.setVisibility(View.VISIBLE);
            honder.applybtn.setVisibility(View.VISIBLE);
            honder.cancelIndent.setVisibility(View.VISIBLE);
            honder.cancelIndent.setText("删除订单");
            honder.applybtn.setText("退款完成");
        }else if (mlist.get(positions).getIndent_state().equals("8")) {
            //退款中
            honder.select.setVisibility(View.VISIBLE);
            honder.applybtn.setVisibility(View.VISIBLE);
            honder.applybtn.setText("退款中");
        }
        else{
            honder.select.setVisibility(View.GONE);
        }

        Log.e("mlist", mlist.get(positions).getCardCartsList().size() + "");
        adapter = new indent_in_list_adapter(mContext, mlist.get(positions).getCardCartsList());
        honder.indent_list.setAdapter(adapter);
        setListViewHeightBasedOnChildren(honder.indent_list);
        honder.all_money.setText(mContext.getString(R.string.indent_all_money)
                        + zhuanhua(Double.parseDouble(mlist.get(positions).getAll_money())
                        + Double.parseDouble(mlist.get(positions).getFreight()))
                        + "("
                        + mlist.get(positions).getInfo() + ")"
//                + String.format(mContext.getString(R.string.freight), mlist.get(positions).getFreight())
        );
        honder.indentNumber.setText("共" + mlist.get(positions).getIndentAllNumber() + mContext.getString(R.string.number_shop));
        if ( mlist.get(positions).getIfFailure().equals("1")) {
            honder.indent_state.setText(OrderInfoActivity.getStatusText(mlist.get(positions).getIndent_state()));
        } else {
            honder.indent_state.setText("已失效");
        }

        final double resultPrice = (Double.parseDouble(mlist.get(positions).getAll_money()) + Double.parseDouble(mlist.get(positions).getFreight()));
        //改为匿名内部实现
        honder.goPayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.onPayClick(mlist.get(positions).getWaitPayPrice(), mlist.get(positions).getIndentId());
                }
            }
        });
        honder.affirm_evaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.pingjia(positions);
                }
            }
        });
        honder.lookLogistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (click != null) {
//                    click.onPayClick(mlist.get(positions).getWaitPayPrice() + "", mlist.get(positions).getIndentId());
//                }
                Intent go_lookLogistics = new Intent(mContext, LogisticsInfoActivity.class);
                go_lookLogistics.putExtra("orderid", mlist.get(positions).getIndentId());
                mContext.startActivity(go_lookLogistics);
            }
        });
        honder.cancelIndent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.onCancelClick(mlist.get(positions).getIndentId());
                }
            }
        });
        honder.tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.tuikuan(positions);
                }
            }
        });
        honder.applybtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.applybtn(positions);
                }
            }
        });
        honder.affirmDelivery.setOnClickListener(new View.OnClickListener() { //确认收货
            @Override
            public void onClick(View v) {
                if (click != null) {
                    click.queren(mlist.get(positions).getIndentId());
                }
            }
        });
        honder.all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    if (mlist.get(positions).getIndent_state().equals("5")) {
                        Toast.makeText(mContext, "取消后的订单无法查看！~", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (mlist.get(positions).getIndent_state().equals("1") && mlist.get(positions).getIfFailure().equals("0")) {
                        Toast.makeText(mContext, "该订单已失效！~", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    click.onjinru(positions);
                }
            }
        });

        return convertView;
    }


    public void setListViewHeightBasedOnChildren(ListView listView) {

        // 获取ListView对应的Adapter

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {

            return;

        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目

            View listItem = listAdapter.getView(i, null, listView);

            listItem.measure(0, 0); // 计算子项View 的宽高

            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度

        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        // listView.getDividerHeight()获取子项间分隔符占用的高度

        // params.height最后得到整个ListView完整显示需要的高度

        listView.setLayoutParams(params);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_indent:
                Log.e("Indent_adapter", "Indent_adapter:取消订单");
                break;
            case R.id.go_payment:
                Log.e("Indent_adapter", "Indent_adapter:去付款");
                break;
            case R.id.all:
                Log.e("Indent_adapter", "Indent_adapter:点击进入");
                break;
            default:
                break;
        }
    }

    public interface onItemMenuClick {
        void onPayClick(double price, String orderid);

        void onCancelClick(String orderid);

        void onjinru(int postion);

        void queren(String orderid);

        void pingjia(int postion);

        void tuikuan(int postion);

        void applybtn(int postion);
    }

    class Honder {


        TextView contactSeller; // 卖家
        TextView cancelIndent;//取消订单
        TextView indent_state;//订单状态
        TextView affirm_evaluate;//去评价
        TextView goPayment;//付款
        TextView indentNumber;//商品总数
        TextView all_money;//商品总额
        TextView tuikuan;//商品总额
        TextView applybtn;//商品退款状态按钮
        ListView indent_list;//商品list
        TextView lookLogistics;//查看物流
        TextView affirmDelivery;//确认收货
        LinearLayout select; //选择是否显示按钮
        //        LinearLayout all; //全部
        com.gensdai.leliang.view.mylistview all; //全部

    }

    private String zhuanhua(Double data) {
//        double money = Double.parseDouble(data);
        DecimalFormat df = new DecimalFormat("0.00");
        return String.format(df.format(data));
    }
}
